<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Index extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        //时间 周
        $before_week = date('Y-m-d',strtotime("-7 day"));
        $after_week = date("Y-m-d",strtotime("+7 day"));
        //时间 月
        $before_month = date("Y-m-d",strtotime("-1 month"));
        $after_month = date("Y-m-d",strtotime("+1 month"));
        //统计
        $user_num = Db::table('lsj_user')->where('created_at','between time',["{$before_week}","$after_week"])->count();
        $week_user_num = Db::table('lsj_user')->count();
        $order_num = Db::table('lsj_order')->where('created_at','between time',["{$before_month}","$after_month"])->count();
        $month_order_num = Db::table('lsj_order')->count();
        $week_user_num = Db::table('lsj_user')->where('created_at','between time',["{$before_week}","$after_week"])->count();
        $balance = Db::table('lsj_order')->where(['status'=>['in','1,2,3,4']])->sum('price');
        $month_balance = Db::table('lsj_order')->where(['status'=>['in','1,2,3,4']])->where('created_at','between time',["{$before_month}","$after_month"])->sum('price');
        $inquiry_num = Db::table('lsj_inquiry_sheet')->count();
        $month_inquiry_num = Db::table('lsj_inquiry_sheet')->where('created_at','between time',["{$before_month}","$after_month"])->count();

        $comments = Db::table('lsj_goods_comment_log a')
            ->join('lsj_user b','a.uid=b.uid')
            ->field('content,nickname,a.created_at')
            ->order('created_at','desc')->limit(10)->select();

        $quotes = Db::table('lsj_user')
            ->where([
                'is_salesman' => 1
            ])
            ->order('quote_num','desc')
            ->field('quote_num,order_num,commission,nickname')
            ->limit(10)
            ->select();

        $card_info = [
            'user_num' => $user_num,
            'week_user_num' => $week_user_num,
            'order_num' => $order_num,
            'month_order_num' => $month_order_num,
            'week_user_num' => $week_user_num,
            'balance' => $balance?$balance:0,
            'month_balance' => $month_balance?$month_balance:0,
            'inquiry_num' => $inquiry_num,
            'month_inquiry_num' => $month_inquiry_num,
        ];

        return $this->fetch('index', ['card_info' => $card_info,'comments' => $comments,'quotes' =>$quotes]);
    }
}